package com.vencent.spring.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vencent.spring.model.Students;

/**
 * show how to transfer data when redirection.
 * for better to test, all request's method is GET.
 * @author 1d
 *
 */
@Controller
@RequestMapping("/gauge")
public class GaugeController {
	/**
	 * After carrying out the Post method, 
	 * always to redirect this Request to prevent repeat post the same Request by user when he is excuting refesh the Page.
	 * But carrying out redirect this Request, the Request's model will be loss, because it's a new Request.
	 * Then Spring provide some special functions to increase the Redirect.
	 */
	
	/**
	 * Avoid this method , when building URL or Sql, this way has Rist.
	 * @param stu
	 * @return
	 */
	@RequestMapping(value="/save", method = RequestMethod.GET)
	public String saveGauge(Students stu){
		return "redirect:/gauge/" + stu.getName();
	}
	
	
	/**
	 * this simple method.
	 * @param stu
	 * @return
	 */
	@RequestMapping(value="/save2", method = RequestMethod.GET)
	public String saveGauge2(Students stu, Model model){
		model.addAttribute("username", stu.getName());
		return "redirect:/gauge/{username}";
	}
	
	/**
	 * this simple method. you can expand it.
	 * this look like a little amazing.
	 * @param stu
	 * @return
	 */
	@RequestMapping(value="/save22", method = RequestMethod.GET)
	public String saveGauge22(Students stu, Model model){
		model.addAttribute("username", stu.getName());
		model.addAttribute("id", stu.getId());
		// if this students's name is wbw, id is 32
		// the following return ,it equal to /gauge/wbw?id=32
		return "redirect:/gauge/{username}";
	}
	
	
	/**
	 * this spring method.
	 * coming on flash. it can handle the whole object rather than some basic value.
	 * the spring is reponsible for taking the first Request's model out and putting it into the Session, 
	 * and then the next Request comeing, 
	 * spring putting it from session to the Second Request's model and destorying the flash where in session.
	 * @param stu
	 * @return
	 */
	@RequestMapping(value="/save3", method = RequestMethod.GET)
	public String saveGauge3(Students stu, RedirectAttributes model){
		model.addAttribute("username", stu.getName());
		model.addFlashAttribute("student", stu);
		return "redirect:/gauge/{username}";
	}
}
